package com.woniuxy.ticketcinema.serviec.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ticketcinema.entity.TicketCinema;
import com.woniuxy.ticketcinema.exception.serviceException.NameExsitException;
import com.woniuxy.ticketcinema.mapper.TicketCinemaMapper;
import com.woniuxy.ticketcinema.serviec.TicketCinemaService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName TicketCinemaServiceImpl
 * @Description TODO
 * @Author zx
 * @Date 2021-02-24 19:24:12
 * @Version 1.0
 */
@Service
public class TicketCinemaServiceImpl extends ServiceImpl<TicketCinemaMapper, TicketCinema> implements TicketCinemaService {

    @Resource
    private TicketCinemaMapper ticketCinemaMapper;

    /**
     * 添加一条影院信息
     *
     * @param ticketCinema
     */
    @Override
    public void add(TicketCinema ticketCinema) {
        //判断数据是否重复
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", ticketCinema.getName());
        if (ticketCinemaMapper.selectCount(queryWrapper) == 0) {
            ticketCinemaMapper.insert(ticketCinema);
        } else {
            throw new NameExsitException("影院名称重复!");
        }
    }

    /**
     * 批量添加一条影院信息
     *
     * @param ticketCinema
     */
    @Override
    public void addAll(List<TicketCinema> ticketCinema) {
        //判断数据是否重复
        QueryWrapper queryWrapper = new QueryWrapper();
        for (TicketCinema i : ticketCinema) {
            queryWrapper.eq("name", i.getName());

        }
        if (ticketCinemaMapper.selectCount(queryWrapper) == 0) {
            for (TicketCinema i : ticketCinema) {
                ticketCinemaMapper.insert(i);
            }
        } else {
            //????是否可以优化
            throw new NameExsitException("名称重复!");
        }
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<TicketCinema> findAll() {
        return ticketCinemaMapper.findAll();
    }


}

